﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "typeconfig.h"
#include "qualityrules.h"
#include <QLabel>
#include <QList>
#include<QString>
#include <QTableWidgetItem>
#include"mytablewidgetitem.h"
#include"cameraconfig.h"
#include"save_image.h"
#include"plcconfig.h"
#include <numeric>


void LabelDisplayMat(QLabel *label, cv::Mat &mat,qreal ax);

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{

    ui->setupUi(this);//不可动摇的地位

    ui->tabWidgetMid->setStyleSheet("QTabBar::tab{background-color:rgb(37, 37, 38);color:rgb(255,255,255)}\
                                  QTabBar::tab::selected{background-color:rgb(45, 45, 48);color:rgb(255,255,255)}\
                                   QTabWidget:pane{border: 3px solid rgb(63, 63, 70); top: -1px;}");
    ui->tabWidgetRight->setStyleSheet("QTabBar::tab{background-color:rgb(37, 37, 38);color:rgb(255,255,255)}\
                                  QTabBar::tab::selected{background-color:rgb(45, 45, 48);color:rgb(255,255,255)}\
                                  QTabWidget:pane{border: 3px solid rgb(63, 63, 70); top: -1px;}");
    ui->groupBox->setStyleSheet("QGroupBox{border: 3px solid transparent}");
    ui->groupBox_2->setStyleSheet("QGroupBox{border: 3px solid transparent;}");
    ui->groupPlayState->setStyleSheet("QGroupBox{border: 3px solid rgb(63, 63, 70);color:rgb(85, 255, 0)}");
    ui->groupProductNews->setStyleSheet("QGroupBox{border: 3px solid rgb(63, 63, 70);color:rgb(85, 255, 0);}");

    QList<QString>defectname={"异物缺陷:","白点缺陷:","白印缺陷:","亮边缺陷:","移位缺陷:","死灯缺陷:","爆灯缺陷:","划伤缺陷:","显示异常:"};//缺陷种类数组
    QList<int> defectnum={1,2,3,4,5,6,7,8,9};                                               //缺陷数量
    QList<QString> defectnumDisplay;                                                         //缺陷数量显示用string数组
    QList<QString> defectrate;

    Graphic_LineSeries(ui->widgetForeignPie,defectname.at(0));//画饼状图
    Graphic_LineSeries(ui->widgetWhiteDotPie,defectname.at(1));//画饼状图
    Graphic_LineSeries(ui->widgetMuraPie,defectname.at(2));//画饼状图
    Graphic_LineSeries(ui->widgetBrightBorderPie,defectname.at(3));//画饼状图

    Graphic_LineSeries(ui->widgetShiftPie,defectname.at(4));//画饼状图
    Graphic_LineSeries(ui->widgetDeadLightPie,defectname.at(5));//画饼状图
    Graphic_LineSeries(ui->widgetBoomLightPie,defectname.at(6));//画饼状图
    Graphic_LineSeries(ui->widgetScratchPie,defectname.at(7));//画饼状图
    Graphic_LineSeries(ui->widgetDisplayUnusualPie,defectname.at(8));//画饼状图

    Graphic_LineSeries(ui->widgetChartCurve,"不良率");//画折线图
    Graphic_BarSet(defectname);//画柱形图
   // Graphic_PieSeries();
   // Graphic_PieSeries2();
  //this->setWindowState(Qt::WindowMaximized);//最大化窗体
  // showMaximized();
  //QWidget::setWindowFlags(Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint | Qt::WindowMinimizeButtonHint);

  //状态栏显示部分
  QLabel *ql=new QLabel(("相机状态:"),this);
  statusBar()->addWidget(ql);
  QLabel *qm=new QLabel(("未连接"),this);
  statusBar()->addWidget(qm);
  QLabel *qlp=new QLabel(("PLC状态:"),this);
  statusBar()->addWidget(qlp);
  QLabel *qmp=new QLabel(("未连接"),this);
  statusBar()->addWidget(qmp);
  QLabel *qlpp=new QLabel(("批次:"),this);
  statusBar()->addWidget(qlpp);
  QLabel *qmpp=new QLabel(("202102221"),this);
  statusBar()->addWidget(qmpp);

//检测缺陷信息显示页面                                                             //缺陷百分比数组
  double defectsum=0;                                                                         //缺陷总数
  for (int i=0;i<defectnum.length();i++){

      defectnumDisplay.append(QString::number(defectnum.at(i)));                       //把缺陷数量int转换string，用于显示
      defectsum += defectnum.at(i);                                                      //得到缺陷总数量
  }
  for (int i=0;i<defectnum.length();i++){

      defectrate.append(QString::number(100*defectnum.at(i)/defectsum,'f',1)+"%");//百分比计算 保留1位小数
  }
  QFont font =  ui->tableWidget->horizontalHeader()->font();                                   //获取字体格式
  font.setBold(true);                                                                          //设置字体加粗

  for (int i=0;i<defectname.length();i++){

      ui->tableWidget->insertRow(i);
      ui->tableWidget->setItem(i, 0, new MyTableWidgetItem((defectname.at(i))));            //第一列插入缺陷种类数据
      ui->tableWidget->setItem(i, 1, new MyTableWidgetItem((defectnumDisplay.at(i))));      //第二列插入缺陷数量
      ui->tableWidget->setItem(i, 2, new MyTableWidgetItem((defectrate.at(i))));            //第三列插入百分比

      ui->tableWidget->item(i,0)->setFont(font);                                                //设置缺陷列(第0列)  为加粗字体
      ui->tableWidget->item(i,1)->setFont(font);                                                //设置缺陷数量(第1列) 为加粗字体
      ui->tableWidget->item(i,2)->setFont(font);                                                //设置百分比(第2列)   为加粗字体

      ui->tableWidget->item(i,1)->setBackgroundColor(QColor(255,255,255));                      //设置缺陷数量(第1列) 背景为白色
      ui->tableWidget->item(i,2)->setBackgroundColor(QColor(255,255,255));                      //设置百分比(第2列)   背景为白色

      ui->tableWidget->item(i,0)->setForeground(QColor(255,255,255));                           //设置缺陷列(第0列)字体颜色为白色

      ui->tableWidget->item(i,0)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);          //设置表中数据居中
      ui->tableWidget->item(i,1)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);          //设置表中数据居中
      ui->tableWidget->item(i,2)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);          //设置表中数据居中
  }
  ui->tableWidget->verticalHeader()->setVisible(false);                                           //设置tableWidget左侧行标签隐藏
  ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);                            //设置tableWidget内容外部不可编辑
  ui->tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{background:#98A9EE;}");//设置表头背景颜色
  ui->tableWidget->horizontalHeader()->setFont(font);                                             //设置表头字体加粗
  ui->tableWidget->setFont(QFont("song", 12));                                                    //设置表头字体大小为12号

  ui->tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);      //自适应列宽
  ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);    //自适应行宽

   //设置NG和ok显示
   //NG 显示
  ui->labelPlayResultInput->setText("NG");
  ui->labelPlayResultInput->setStyleSheet("color:red;font:40pt");
  ui->labelPlayResultInput->setAlignment(Qt::AlignCenter);
  //OK显示
  ui->labelPlayResultInput->setText("OK");
  ui->labelPlayResultInput->setStyleSheet("color:green;font:40pt");
  ui->labelPlayResultInput->setAlignment(Qt::AlignCenter);

  //设置相机拍摄图片显示
  std::string SRC_PATH ="D:\\NewUI\\BackLight0520\\example\\";
  cv::Mat src_ceguang1_Temp;
  cv::Mat src_ceguang_right_Temp;
  cv::Mat src_ceguang_left_Temp;
  src_ceguang1_Temp = cv::imread(SRC_PATH+"B0ROI.bmp", -1);
  src_ceguang_right_Temp=cv::imread(SRC_PATH+"BRROI.bmp", -1);
  src_ceguang_left_Temp=cv::imread(SRC_PATH+"BLROI.bmp", -1);
  LabelDisplayMat(ui->labelCameraMain,src_ceguang1_Temp,-90);
  LabelDisplayMat(ui->labelCameraRight,src_ceguang_right_Temp,-90);
  LabelDisplayMat(ui->labelCameraLeft,src_ceguang_left_Temp,-90);

}
void LabelDisplayMat(QLabel *label, cv::Mat &mat,qreal ax)//qreal是qt的double类型
{
    cv::Mat Rgb;
    QImage Img;
    QMatrix matrix;
    matrix.rotate(ax);
    if (mat.channels() == 3)//RGB Img
    {
        cv::cvtColor(mat, Rgb, CV_BGR2RGB);//颜色空间转换
        Img = QImage((const uchar*)(Rgb.data), Rgb.cols, Rgb.rows, Rgb.cols * Rgb.channels(), QImage::Format_RGB888);
    }
    else//Gray Img
    {
        Img = QImage((const uchar*)(mat.data), mat.cols, mat.rows, mat.cols*mat.channels(), QImage::Format_Indexed8);
    }
    Img=Img.transformed(matrix,Qt::FastTransformation);
    label->setAlignment(Qt::AlignCenter);
    label->setScaledContents(true);//图片自适应label大小
    Img=Img.scaled(label->width(),label->height(),Qt::IgnoreAspectRatio, Qt::SmoothTransformation); // 饱满填充
   // Img=Img.scaled(label->width(),label->height(),Qt::KeepAspectRatio, Qt::SmoothTransformation); // 饱满填充
    label->setPixmap(QPixmap::fromImage(Img));

    //表格数据显示



}
MainWindow::~MainWindow()
{
    delete ui;
}

//折线图
void MainWindow::Graphic_LineSeries(QChartView *widgetMid,QString title)
{
    QLineSeries *series = new QLineSeries();
    QValueAxis *LineSeries_AxisY;
    QValueAxis *LineSeries_AxisX;

    LineSeries_AxisY = new QValueAxis;
    LineSeries_AxisY->setRange(0,5);
    LineSeries_AxisX = new QValueAxis;
    LineSeries_AxisX->setRange(1,4);
    LineSeries_AxisX->setTickCount(3);//设置显示点个数
    LineSeries_AxisX->setLabelFormat("%d");
    series->append(0,1);
    series->append(1,1.1);
    series->append(2,2);
  //  series->setPen(QPen(QColor(244,232,70)));//设置折线图数据连接线的颜色
    QPen pen;
    pen.setWidth(5);
    pen.setColor(QColor(244,232,70));
    series->setPen(pen);
    QChart *chart2 = new QChart();
    chart2->addSeries(series);
    chart2->setAxisY(LineSeries_AxisY, series);
    chart2->setAxisX(LineSeries_AxisX, series);

    //chart2->setTheme(QChart::ChartThemeQt);
    chart2->setTitleBrush(QColor(255,255,255)); //设置标题颜色
    chart2->setTitle(title);
    chart2->axisX()->setLinePen(QPen(Qt::SolidLine));//设置坐标轴的格式
    chart2->axisY()->setLinePen(QPen(Qt::SolidLine));//设置坐标轴的格式
    chart2->axisX()->setLabelsColor(QColor(43,229,235));//设置坐标轴字体颜色
    chart2->axisY()->setLabelsColor(QColor(43,229,235));//设置坐标轴字体颜色
    chart2->axisX()->setLabelsFont(QFont("song", 6));//设置坐标轴字体
    chart2->axisY()->setLabelsFont(QFont("song", 6));//设置坐标轴字体
    chart2->axisX()->setTitleText("产品数量/个");//设置坐标轴字体
    chart2->axisX()->setTitleBrush(QColor(255,255,255));//设置坐标轴字体
    chart2->axisY()->setTitleText("不良率/%");//设置坐标轴字体
    chart2->axisY()->setTitleBrush(QColor(255,255,255));
    chart2->axisX()->setTitleFont(QFont("song", 6));
    chart2->axisY()->setTitleFont(QFont("song", 6));
    chart2->axisX()->setLinePenColor(Qt::white);
    chart2->axisY()->setLinePenColor(Qt::white);
    chart2->axisX()->setGridLineVisible(false);//设置背景线显示
    chart2->axisY()->setGridLineVisible(false);//设置背景线显示
    chart2->legend()->setVisible(true);
    chart2->legend()->setAlignment(Qt::AlignBottom);
    chart2->setMargins(QMargins(0,0,0,0));         //设置显示区域与框体边界
//    chart2->setAnimationOptions(QChart::AllAnimations);//动画效果
    chart2->legend()->hide();
    chart2->setBackgroundVisible(false);//设置曲线显示背景色false为透明
    widgetMid->setChart(chart2);
    widgetMid->setRenderHint(QPainter::Antialiasing);
}

//饼状图

void MainWindow::drawPie(QChartView *widgetMid,QString title)
{

    QPieSeries *series2 = new QPieSeries();
    series2->setPieSize(1);//设置扇形区域的距离=R-r
    series2->setHoleSize(0.7);//设置扇形区域挖孔的占比0~1
    series2->append("1",1);
    series2->append("d",9);

    //QPainter painter(this);
    //painter.drawPie()
    //series->setLabelsVisible();
    QPieSlice *slice_Fail2 = series2->slices().at(0);
    QPieSlice *slice_Success2 = series2->slices().at(1);

    // 设置这片是否和饼状图分开
    //slice_Fail2->setExploded();
    // 设置分开的距离为0.5
    slice_Fail2->setExplodeDistanceFactor(0.05);
    slice_Fail2->setColor(QColor(118,118,118));//灰色
    slice_Success2->setColor(QColor(62,197,86));//绿色
    slice_Success2->setBorderColor(QColor(62,197,86));
    slice_Fail2->setBorderColor(QColor(118,118,118));//边框
    QChart *chart2 = new QChart();
    chart2->addSeries(series2);
    chart2->setTitleBrush(QColor(255,255,255));
    chart2->legend()->hide();
    chart2->setBackgroundVisible(false);

    chart2->setMargins(QMargins(0,0,0,0));
    widgetMid->setAlignment( Qt::AlignVCenter);

    chart2->resize(widgetMid->width(),widgetMid->height()); // 饱满填充
    widgetMid->setChart(chart2);
    widgetMid->setRenderHint(QPainter::Antialiasing);
    //显示饼状图的百分比
}
//柱形图
void MainWindow::Graphic_BarSet(QList<QString>detectorname)
{

    QBarSet *set0 = new QBarSet("Jane");
    QBarSet *set1 = new QBarSet("John");//每一个Qbarset是一个标签显示的柱形图个数
    *set0 << 1 << 2 <<3<<4<<5<<6<<7<<8<<9;
    *set1 << 1 << 2 <<3<<4<<5<<6<<7<<8<<9;
   set0->setColor(QColor(32,70,230));
   set0->setBorderColor(QColor(32,70,230));
   set1->setColor(QColor(240,110,15));
   set1->setBorderColor(QColor(240,110,15));

    QBarSeries *series = new QBarSeries();
    series->append(set0);
    series->append(set1);
    QValueAxis *LineSeries_AxisY;//纵坐标
    LineSeries_AxisY = new QValueAxis;
    LineSeries_AxisY->setRange(0,20);
    LineSeries_AxisY->setGridLineVisible(false);

    QBarCategoryAxis *axisX = new QBarCategoryAxis();//横坐标

    axisX->setGridLineVisible(false);
    QChart *chart = new QChart();
    chart->addSeries(series);
    axisX->append(detectorname);
    axisX->setTitleFont(QFont("song",1));
    chart->setAxisY(LineSeries_AxisY, series);
    chart->setAxisX(axisX, series);//显示坐标轴刻度

    chart->legend()->setVisible(true);
    chart->legend()->setAlignment(Qt::AlignBottom);
    chart->setMargins(QMargins(0,0,0,0));
   // chart->setAnimationOptions(QChart::AllAnimations);//动画效果
    chart->legend()->hide();
    chart->setBackgroundVisible(false);

    chart->axisX()->setLabelsColor(QColor(255,255,255));
    chart->axisY()->setLabelsColor(QColor(255,255,255));//坐标轴字体颜色
    ui->widgetColumn->setChart(chart);
    ui->widgetColumn->setRenderHint(QPainter::Antialiasing);

}


void MainWindow::on_actionTypeConfig_triggered()
{
    typeconfig* d=new typeconfig();
    d->setWindowTitle("产品型号配置");
    d->show();
}

void MainWindow::on_actionQualityRank_triggered()
{
    QualityRules* s=new QualityRules();
     s->setWindowTitle("质量规则配置");
    s->show();
}

void MainWindow::on_actionCameraConfig_triggered()
{
    CameraConfig*d=new CameraConfig();
    d->setWindowTitle("相机配置");
    d->setFixedSize(600,500);
    d->show();
}

void MainWindow::on_actionSave_triggered()
{
    Save_image* s=new Save_image();
    s->setWindowModality(Qt::ApplicationModal);//设置窗口基涅
    s->setWindowFlags(windowFlags()& ~Qt::WindowMaximizeButtonHint);
    s->setFixedSize(600,500);
    s->setWindowTitle("保存");
    s->show();

}

void MainWindow::on_actionPLC_triggered()
{
    PlcConfig* plcConfig = new PlcConfig ();
    plcConfig->setWindowTitle("PLC配置");
    plcConfig->setFixedSize(600,500);
    plcConfig->show();
}
